package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.GetSingleLiveVideoRecordListResponse;
import com.molyfun.parents.rest.bean.GetSubjectResponse;
import com.molyfun.parents.zhibo.playback.PlayBackActivity;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ClassVideoRecordView extends LinearLayout implements View.OnClickListener, View.OnTouchListener {

    protected LayoutInflater layoutInflater;

    @InjectView(R.id.teacher_name)
    TextView mTeacherName;
    @InjectView(R.id.product_name)
    TextView mProductName;
    @InjectView(R.id.cover)
    ImageView mCover;
    @InjectView(R.id.line)
    View mLine;
    @InjectView(R.id.view_video_btn)
    TextView mViewVideoBtn;
    @InjectView(R.id.view_time)
    TextView mViewTimes;

    private Context context;
    private GetSingleLiveVideoRecordListResponse.SingleLiveVideoRecordBean mSingleLiveVideoRecordBean;

    public ClassVideoRecordView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
        init(context, attrs);
    }

    public ClassVideoRecordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ClassVideoRecordView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_class_video_record, this);
        ButterKnife.inject(this);

        mViewVideoBtn.setOnClickListener(this);
    }

    public void initData(GetSingleLiveVideoRecordListResponse.SingleLiveVideoRecordBean singleLiveVideoRecordBean) {
        initData(singleLiveVideoRecordBean, false);
    }

    public void initData(GetSingleLiveVideoRecordListResponse.SingleLiveVideoRecordBean singleLiveVideoRecordBean, boolean isLast) {
        if (singleLiveVideoRecordBean == null) {
            return;
        }
        mSingleLiveVideoRecordBean = singleLiveVideoRecordBean;
        mTeacherName.setText(singleLiveVideoRecordBean.getUsername());
        mProductName.setText(singleLiveVideoRecordBean.getName());
        mViewTimes.setText(singleLiveVideoRecordBean.getOpentimes() + "观看次数");
//        setOnClickListener(this);
        if(!TextUtils.isEmpty(singleLiveVideoRecordBean.getHeadimgurl())){
            Picasso.with(context)
                    .load(singleLiveVideoRecordBean.getHeadimgurl())
                    .placeholder(R.mipmap.ic_team_default)
                    .into(mCover);
        }else{
            mCover.setImageResource(R.mipmap.ic_team_default);
        }

        if(isLast){
            mLine.setVisibility(View.GONE);
        }else{
            mLine.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://0
                return false;
            case MotionEvent.ACTION_UP:
                PlayBackActivity.startActivity(getContext(), mSingleLiveVideoRecordBean.getId(), mSingleLiveVideoRecordBean.getName(), mSingleLiveVideoRecordBean.getRecordurl(), false);
//                PlayBackActivity.startActivity();
//                ClassRoomActivity.startActivity(getContext(), mTeamBean);
//                ProductDetailActivity.startActivity(getContext(), mSubjectBrief.getId(), PreferencesStorageUtil.getStudentId(getContext()), Const.CHATROWPRODUCT);
                return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                getPlaybackUrl();
                break;
        }
    }

    private void getPlaybackUrl(){
        Call<GetSubjectResponse> call = MFPApplication.getInstance().getRestClient().getSubjectCatalogInfo(mSingleLiveVideoRecordBean.getId());
        call.enqueue(new Callback<GetSubjectResponse>() {
            @Override
            public void onResponse(Call<GetSubjectResponse> call, Response<GetSubjectResponse> response) {
                if (response.body() != null) {
                    if (response.body().getCode() == 200 && response.body().getData() != null) {
                        PlayBackActivity.startActivity(getContext(), mSingleLiveVideoRecordBean.getId(), mSingleLiveVideoRecordBean.getName(), response.body().getData().getRecordurl(), false);
                    }
                }
            }

            @Override
            public void onFailure(Call<GetSubjectResponse> call, Throwable t) {
            }
        });
    }

}